import React from 'react';
import PropTypes from 'prop-types';
import {Card, Button, Row, Col, Divider} from 'antd';
import {DATETIME_FORMAT, FILES_API} from '../constants';
import {formatDateTime, conventEnumValueToString} from '../utils';
import DescriptionList from 'ant-design-pro/lib/DescriptionList';
import {hasPermissions} from './utils';
import WrappedPopconfirm from '../common/WrappedPopconfirm';
import {PERMISSION} from './constants';
import {baseDataStatus} from '../Enum';
import routes from './routes';
import {abandonData, getDetail} from './api';
import styles from './style.css';
const {Description} = DescriptionList;

export class DetailPanel extends React.PureComponent {
    state = {
        loading: false,
        data: {}
    };
    componentDidMount() {
        this.getDetailInfo();
    }
    getDetailInfo = () => {
        const id = this.props.id;
        this.setState({loading: true});
        getDetail(id)
            .then(res => {
                if(res.ok)
                    this.setState({
                        loading: false,
                        data: res.data,
                    });
                else
                    this.setState(({
                        loading: false
                    }));
            });
    }
    handleUpdateClick = () => {
        this.props.history.push(routes.update.format(this.props.id));
    }
    abandonConfirm = () => {
        this.setState({loading: true});
        const rowVersion = this.data.rowVersion;
        abandonData(this.data.id, rowVersion)
            .then(res => {
                this.setState({loading: false});
                if(res.ok)
                    this.props.successReturn();
            });
    }
    
    render() {
        const detailData = this.state.data || {};
        const {options, logoAttachments = [], signatureAttachments = []} = detailData;
        const {formatMessage} = this.props.intl;
        const operations = [];
        if(hasPermissions(this.props.permissions, PERMISSION.update) && hasPermissions(options, PERMISSION.update))
            operations.push(<Button key="update" onClick={this.handleUpdateClick} size="small">{
                formatMessage({
                    id: 'detailPage.btn.update',
                    defaultMessage: '编辑'
                })
            }</Button>);
        if(hasPermissions(this.props.permissions, PERMISSION.abandon) && hasPermissions(options, PERMISSION.abandon))
            operations.push(
                <WrappedPopconfirm
                    id="abandon"
                    key="abandon"
                    title={formatMessage({
                        id: 'detailPage.popconfirm.message',
                        defaultMessage: '是否继续?'
                    })}
                    onConfirm={this.abandonConfirm}>
                    <Button key="abandon" size="small">{
                        formatMessage({
                            id: 'detailPage.btn.abandon',
                            defaultMessage: '作废'
                        })
                    }</Button>
                </WrappedPopconfirm>);

        return (
            <div className={styles.detail}>
                <Card loading={this.state.loading}>
                    <DescriptionList size="large" title={<Row><Col span={12}>{
                        formatMessage({
                            id: 'detailPage.card.tradeMethodmessage',
                            defaultMessage: '基本信息'
                        })
                    }</Col>
                    <Col className={`col-align-right ${styles.detail}`} span={12}>{operations}</Col>
                    </Row>}>
                        <Description term={formatMessage({
                            id: 'detailPage.data.code',
                            defaultMessage: '编号',
                        })}>{detailData.code}</Description>
                        <Description term={formatMessage({
                            id: 'detailPage.data.name',
                            defaultMessage: '名称'
                        })}>{detailData.name}</Description>
                        <Description term={formatMessage({
                            id: 'detailPage.data.engName',
                            defaultMessage: '公司英文名称',
                        })}>{detailData.engName}</Description>
                        <Description term={formatMessage({
                            id: 'detailPage.data.address',
                            defaultMessage: '地址'
                        })}>{detailData.address}</Description>
                        <Description term={formatMessage({
                            id: 'detailPage.data.contactNumber',
                            defaultMessage: '电话',
                        })}>{detailData.contactNumber}</Description>
                        <Description term={formatMessage({
                            id: 'detailPage.data.fax',
                            defaultMessage: '传真'
                        })}>{detailData.fax}</Description>
                        <Description term={formatMessage({
                            id: 'detailPage.data.status',
                            defaultMessage: '状态'
                        })}>{conventEnumValueToString(baseDataStatus, detailData.status)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPage.data.creatorName',
                            defaultMessage: '创建人'
                        })}>{detailData.creatorName}</Description>
                        <Description term={formatMessage({
                            id: 'detailPage.data.createTime',
                            defaultMessage: '创建时间'
                        })}>{formatDateTime(detailData.createTime, DATETIME_FORMAT)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPage.data.modifierName',
                            defaultMessage: '修改人'
                        })}>{detailData.modifierName}</Description>
                        <Description term={formatMessage({
                            id: 'detailPage.data.modifyTime',
                            defaultMessage: '修改时间'
                        })}>{formatDateTime(detailData.modifyTime, DATETIME_FORMAT)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPage.data.abandonerName',
                            defaultMessage: '作废人'
                        })}>{detailData.abandonerName}</Description>
                        <Description term={formatMessage({
                            id: 'detailPage.data.abandonTime',
                            defaultMessage: '作废时间'
                        })}>{formatDateTime(detailData.abandonTime, DATETIME_FORMAT)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPage.data.remark',
                            defaultMessage: '备注'
                        })}>{detailData.remark}</Description>
                    </DescriptionList>
                    <Divider/>
                    {logoAttachments && logoAttachments.length > 0
                        ? <DescriptionList title={formatMessage({
                            id: 'detailPage.card.logoAttachment',
                            defaultMessage: '附件logo清单'
                        })} size="large">
                            {logoAttachments.map(item => (<Description key={item.fileId}>
                                <a icon="download" download href={`${FILES_API.files}/${item.fileId}`} size="small">
                                    {item.fileName}</a></Description>))}
                        </DescriptionList>
                        : formatMessage({
                            id: 'detailPage.content.noLogoAttachment',
                            defaultMessage: '暂无logo附件'
                        })}
                    {signatureAttachments && signatureAttachments.length > 0
                        ? <DescriptionList title={formatMessage({
                            id: 'detailPage.card.signatureAttachment',
                            defaultMessage: '签章附件清单'
                        })} size="large">
                            {signatureAttachments.map(item => (<Description key={item.fileId}>
                                <a icon="download" download href={`${FILES_API.files}/${item.fileId}`} size="small">
                                    {item.fileName}</a></Description>))}
                        </DescriptionList>
                        : formatMessage({
                            id: 'detailPage.content.noStampAttachment',
                            defaultMessage: '暂无签章附件'
                        })}
                </Card>
                <div className="page-toolbar">
                    <Button
                        type="primary"
                        disabled={this.state.loading}
                        onClick={this.props.onCancel}>{formatMessage({
                            id: 'detailPage.btn.return',
                            defaultMessage: '返回'
                        })}</Button>
                </div>
            </div>
        );
    }
}

DetailPanel.propTypes = {
    id: PropTypes.string.isRequired,
    permissions: PropTypes.array.isRequired,
    onCancel: PropTypes.func.isRequired,
    history: PropTypes.object,
    intl: PropTypes.object,
    successReturn: PropTypes.func,
};

import {connect} from 'react-redux';
import {tableSearch} from './actions';
import {injectIntl} from './intl';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
const getPermissions = selectorFactory(['page', 'domainData', 'permission', 'data']);
const mapStateToProps = (state, props) => ({
    id: props.id,
    permissions: getPermissions(state),
});
const mapDispatchToProps = (dispatch, props) => ({
    onCancel: () => {
        props.history.push(routes.query.url());
    },
    successReturn: () => {
        dispatch(tableSearch());
        props.history.push(routes.query.url());
    },
});
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(DetailPanel));
